<?php

namespace app\admin\controller\mall;

use app\admin\service\TriggerService;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\lib\ArrClass;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MallShop;
use app\common\model\SystemConfig;
use think\App;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;

/**
 * @ControllerAnnotation(title="店铺管理")
 */
class Shop extends AdminController
{

    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new MallShop();

        $this->assign('getStatusList', $this->model->getStatusList());

    }

    /**
     * @NodeAnotation (title="动态获取地址")
     * @return array|\think\response\Json
     * @throws \ReflectionException
     * @throws \think\Exception
     */
    public function getShopByAddress()
    {
        $keywords = $this->request->param('keywords', '', 'trim');
        $city = $this->request->param('city', '', 'trim');
        if (empty($keywords)) {
            return Show::json_show(Status::mini_error, "请输入搜索词");
        }
        $type = sysconfig('map', 'map_type');
        $key_id = sysconfig('map', 'map_local_key_id');
        $key = ArrClass::mapAttr();
        $obj = ArrClass::initClass($type, $key, [$key_id], true);
        $result = $obj->suggestion($keywords, $city);
        if ($result['code'] == Status::status_normal) {
            return Show::json_show(Status::status_normal, "ok", $result['data']);
        } else {
            return Show::json_show(Status::status_error, $result['msg']);
        }
    }

    public function getShopByList()
    {
        $where = [
            'status' => 1
        ];
        $row = $this->model->getBaseByNormalList($where, 'id,title', 'sort desc,id desc');
        return Show::json_show(Status::status_normal, 'ok', $row);
    }

    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            if (empty($post['address'])) {
                $post['address'] = $post['old_address'];
            }
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation (title="授权打印机")
     * @return void
     * @throws \think\Exception
     */
    public function machine($id)
    {
        if ($this->request->post()) {
            $machine_id = $this->request->param('machine_id', '', 'trim');
            $express_id = $this->request->param('express_id', '', 'trim');
            if (empty($machine_id)) {
                $this->error("请输入打印机编号");
            }
            $res = $this->model->updateDataSave($id, [
                "machine_id" => $machine_id,
                "express_id" => $express_id,
                "update_time" => time()
            ]);
            if ($res) {
                $this->success("保存成功");
            } else {
                $this->error("保存失败");
            }
        }
        $row = $this->model->getBaseById($id);
        if (empty($row)) {
            $this->error("信息不存在");
        }
        return $this->fetch('', [
            "row" => $row
        ]);
    }

    /**
     * @NodeAnotation(title="添加总店打印机")
     * @return mixed
     */
    public function print()
    {
        if ($this->request->isPost()) {
            $title = $this->request->param('title', '', 'trim');
            (new SystemConfig())->where('name', "order_machine")
                ->update([
                    'value' => $title,
                ]);
            TriggerService::updateSysconfig();
            $this->success("设置成功");
        }
        $row = [
            'title' => sysconfig('shop', 'order_machine')
        ];
        return $this->fetch('', [
            'row' => $row
        ]);
    }
}